package com.web.oa.shiro;


import com.web.oa.pojo.ActiveEmployee;
import com.web.oa.pojo.Employee;
import com.web.oa.pojo.MenuTree;
import com.web.oa.pojo.SysPermission;
import com.web.oa.service.EmployeeService;
import com.web.oa.service.SysService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private SysService sysService;

    @Autowired
    private EmployeeService employeeService;

    //    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String name = (String) authenticationToken.getPrincipal();
        Employee employee = null;
        try {
            employee = employeeService.findEmployeeByName(name);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (employee == null){
            return null;
        }

        List<MenuTree> menuTrees =sysService.findMenuTree();
        //把员工的身份信息封装
        ActiveEmployee activeEmployee = new ActiveEmployee();
        activeEmployee.setId(employee.getId());
        activeEmployee.setEmployeeId(employee.getId());
        activeEmployee.setName(employee.getName());
        activeEmployee.setManageId(employee.getManagerId());
        activeEmployee.setMenuTrees(menuTrees);

        return new SimpleAuthenticationInfo
                (activeEmployee, employee.getPassword(),
                        ByteSource.Util.bytes(employee.getSalt()),
                        "认证");
    }

    //    授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        ActiveEmployee  activeEmployee = (ActiveEmployee) principalCollection.getPrimaryPrincipal();

        List<SysPermission> permissions = null;
        try {
            permissions = sysService.findPermissionListByEmployeeId(activeEmployee.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (permissions == null){
            return null;
        }

        List<String> permissionList = new ArrayList<>();
        for (SysPermission permission : permissions) {
            permissionList.add(permission.getPercode());
        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissionList);

        return simpleAuthorizationInfo;
    }
}
